package com.whut.rssReader.activity;

import android.content.ContentValues;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.whut.rssReader.db.RssInfo;
import com.whut.rssReader.entity.Resource;

public class SortAddDialog extends MyDialog {
	
	Button btn_add_sort;
	Button btn_add_cancle;
	EditText et_sort_name;
	private Resource resource;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Intent intent = getIntent();
		resource = (Resource) intent.getSerializableExtra(Resource.KEY);
		findView();
	}
	
	@Override
	public View getPageView() {
		LayoutInflater layoutInflater = LayoutInflater.from(this);
		View pageView = layoutInflater.inflate(R.layout.add_sort_dialog, null);
		return pageView;
	}
	
	private void findView(){
		btn_add_sort = (Button) findViewById(R.id.bt_add_sort);
		btn_add_cancle = (Button) findViewById(R.id.bt_cancle_sort);
		et_sort_name = (EditText) findViewById(R.id.add_sort_name);
		
		if(resource != null){
			et_sort_name.setText(resource.name);
		}
		btn_add_cancle.setOnClickListener(this);
		btn_add_sort.setOnClickListener(this);
	}
	
	@Override
	public void onClick(View v) {
		switch(v.getId()){
		case R.id.bt_add_sort:
			String sortName = et_sort_name.getText().toString();
			if(TextUtils.isEmpty(sortName)){
				et_sort_name.setError("Please enter a String");
				return;
			}
			System.out.println(et_sort_name.getText().toString());
			ContentValues contentValue = new ContentValues();
			contentValue.put(RssInfo.SORT_NAME, et_sort_name.getText().toString());
			if(resource != null){
				contentResolver.update(RssInfo.CONTENT_URI_SORT, contentValue
						, RssInfo.SORT_ID + "=" + resource.id, null);
			}else{
				contentResolver.insert(RssInfo.CONTENT_URI_SORT, contentValue);
			}
			this.finish();
			break;
		case R.id.bt_cancle_sort:
			this.finish();
			break;
		}
	}

}
